/*! \file RBTreeNode.inl
 *	\brief 
 *
 *	created:	2010/11/23
 *	created:	23:11:2010   12:51
 *	filename: 	C:\src\tactics-engine\libs\core\impl\RBTreeNode.inl
 *	file path:	C:\src\tactics-engine\libs\core\impl
 *	file base:	RBTreeNode
 *	file ext:	inl
 *	author:		Vitalii Trubchaninov
 *				vitalii.trubchaninov@ubisoft.com
 */
namespace tt{
namespace core{

namespace impl{

template<typename ValueType>
RBTreeNode<ValueType>::RBTreeNode(Self* _parent, const value_type& _value)
: parent(_parent)
, left(NULL)
, right(NULL)
, color(RED)
, value(_value)
{

}

template<typename ValueType>
RBTreeNode<ValueType>::~RBTreeNode()
{
}

template<typename ValueType>
bool RBTreeNode<ValueType>::operator<(const Self& other)
{
	return value.first < other.value.first;
}

template<typename ValueType>
bool RBTreeNode<ValueType>::operator==(const Self& other)
{
	return value.first == other.value.first;
}

template<typename ValueType>
RBTreeNode<ValueType>* RBTreeNode<ValueType>::bigest()
{
	Self* current = this;
	while(current->right)
	{
		current = current->right;
	}
	return current;
}

template<typename ValueType>
RBTreeNode<ValueType>* RBTreeNode<ValueType>::smallest()
{
	Self* current = this;
	while(current->left)
	{
		current = current->left;
	}
	return current;
}

template<typename ValueType>
RBTreeNode<ValueType>* RBTreeNode<ValueType>::next()
{
	if (right)
		return right->smallest();

	Self* current = this;
	Self* current_parent = parent;
	while(current_parent && current_parent->right == current)
	{
		current = current_parent;
		current_parent = current_parent->parent;
	}
	return current_parent;
}

template<typename ValueType>
RBTreeNode<ValueType>* RBTreeNode<ValueType>::prev()
{
	if (left)
		return left->bigest();

	Self* current = this;
	Self* current_parent = parent;
	while(current_parent && current_parent->left == current)
	{
		current = current_parent;
		current_parent = current_parent->parent;
	}
	return current_parent;
}

} //namespace impl

} //namespace core
} //namespace tt